package classes.pack;

import java.util.Vector;

import android.location.Location;
import android.util.Log;

import com.google.android.maps.GeoPoint;

public class ParkingZones 
{
	private static Vector<ParkingZone> demoParkingZones;
	
	public ParkingZones()
	{
		ParkingZones.demoParkingZones = new Vector<ParkingZone>();
		//fillDemoParkingZones();
		loadParkingZones();
	}
	
	public static void fillDemoParkingZones()
	{
		//list of parking zones
		demoParkingZones.add(new ParkingZone(new GeoPoint(52642884, 4725424), 1001, 60));
		demoParkingZones.add(new ParkingZone(new GeoPoint(52641052, 4725632), 9009, 20));
		demoParkingZones.add(new ParkingZone(new GeoPoint(52712767, 5264045), 1234, 10));//Mitchels house
		demoParkingZones.add(new ParkingZone(new GeoPoint(52321367, 4881169), 1234, 50));//Logica Amstelveen

		
	}
	
	public static void loadParkingZones()
	{
		//load parking zones from xml
		demoParkingZones.add(new ParkingZone(new GeoPoint(52642884, 4725424), 1001, 60));
		demoParkingZones.add(new ParkingZone(new GeoPoint(52641052, 4725632), 9009, 20));
		demoParkingZones.add(new ParkingZone(new GeoPoint(52712767, 5264045), 1234, 10));//Mitchels house
	}
	
	public static boolean inParkingZone(GeoPoint g)
	{
		
		double distance;  
		Location locationA = new Location("point A");  

		locationA.setLatitude(g.getLatitudeE6()/ 1e6);  
		locationA.setLongitude(g.getLongitudeE6()/ 1e6);  
		
		for(int i = 0; i<demoParkingZones.size(); i++)
		{
			
			Location locationB = new Location("point B");  
			
					
			locationB.setLatitude(demoParkingZones.get(i).getPosition().getLatitudeE6()/ 1e6);  
			locationB.setLongitude(demoParkingZones.get(i).getPosition().getLongitudeE6()/ 1e6);  
						
			distance = locationA.distanceTo(locationB);
			
			Log.i("distance", Double.toString(distance));//distance for testing
			if(distance < demoParkingZones.get(i).getMargin())
			{
				return true;
			}
		}
		
		return false;		
	}
	
	public static int getParkingZone(GeoPoint position)
	{
		//returns the parking zone near the position
				
		double distance;  
		Location locationA = new Location("point A");  

		locationA.setLatitude(position.getLatitudeE6()/ 1e6);  
		locationA.setLongitude(position.getLongitudeE6()/ 1e6);  
		
		for(int i = 0; i<demoParkingZones.size(); i++)
		{
			
			Location locationB = new Location("point B");  
			
					
			locationB.setLatitude(demoParkingZones.get(i).getPosition().getLatitudeE6()/ 1e6);  
			locationB.setLongitude(demoParkingZones.get(i).getPosition().getLongitudeE6()/ 1e6);  
						
			distance = locationA.distanceTo(locationB);
			
			//Log.i("distance", Double.toString(distance));//distance for testing
			if(distance < demoParkingZones.get(i).getMargin())
			{
				return demoParkingZones.get(i).getZoneId();
			}
		}
				
		return 0;
	}
	
	public static int getCurrentParkingZone()
	{
		//Take currentParkingZone or make default
		//Linked with SmsInterface
		
		GeoPoint currentPos = Logging.getLatestPosition().getLocation();
		
		double distance;  
		Location locationA = new Location("point A");  

		locationA.setLatitude(currentPos.getLatitudeE6()/ 1e6);  
		locationA.setLongitude(currentPos.getLongitudeE6()/ 1e6);  
		
		for(int i = 0; i<demoParkingZones.size(); i++)
		{
			
			Location locationB = new Location("point B");  
			
					
			locationB.setLatitude(demoParkingZones.get(i).getPosition().getLatitudeE6()/ 1e6);  
			locationB.setLongitude(demoParkingZones.get(i).getPosition().getLongitudeE6()/ 1e6);  
						
			distance = locationA.distanceTo(locationB);
			
			//Log.i("distance", Double.toString(distance));//distance for testing
			if(distance < demoParkingZones.get(i).getMargin())
			{
				return demoParkingZones.get(i).getZoneId();
			}
		}
				
		return 0;
	}
	
	

}
